{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b7d2d4ca",
   "metadata": {},
   "source": [
    "---\n",
    "* 日期：2022-11-20\\week13（周三）\n",
    "* 课程：python\n",
    "* 记录人：zeng\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19b03bfb",
   "metadata": {},
   "source": [
    "# 一、字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "498436a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:wuhuichun\n",
      "a was found 0 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 1 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 3 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57fc2542",
   "metadata": {},
   "source": [
    "## *初始化元音的频度计数*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a984dd7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:u\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'u'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_29744/121475301.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvowels\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m         \u001b[0mfound\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mletter\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msorted\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfound\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'was found'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mv\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'time(s).'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'u'"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')\n",
    "\n",
    "# 字典的键没有初始化，会出现“KeyError”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aafc8cba",
   "metadata": {},
   "source": [
    "# 二、集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "040730b9",
   "metadata": {},
   "source": [
    "## *set 函数*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "207c8458",
   "metadata": {},
   "source": [
    "* 从序列创建集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1a232d88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 未使用set函数前\n",
    "vowels = {'a','e','e','i','o','u','u'}\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b33680c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用set函数后\n",
    "vowels = set('aeeiouu')\n",
    "vowels"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c20ae08",
   "metadata": {},
   "source": [
    "## *union合并集合（并集）*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "10985128",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'\n",
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "163582ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u_list = sorted(list(u))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f7cba4b",
   "metadata": {},
   "source": [
    "## *difference指出共有元素（差集）*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c678736d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "597cd1c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'h', 'l'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = set(word).difference(vowels)\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c9136d2",
   "metadata": {},
   "source": [
    "## *intersection报告共同对象（交集）*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "cb993aa6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f00a557",
   "metadata": {},
   "source": [
    "* 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "aa235c4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:zeng\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowels in found:\n",
    "    print(vowels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "728d3908",
   "metadata": {},
   "source": [
    "# 三、元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cf5ff81",
   "metadata": {},
   "source": [
    "## *元组和列表的比较*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b3c26a56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表\n",
    "vowels1 = ['a','e','i','o','u']\n",
    "type(vowels1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a8ecc6e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组\n",
    "vowels2 = ('a','e','i','o','u')\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7afe603",
   "metadata": {},
   "source": [
    "## *元组是不可变的*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "117fa2ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'I', 'o', 'u']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表可变\n",
    "vowels1[2] = 'I'\n",
    "vowels1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1bde06da",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_29744/813889600.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 元组不可变\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mvowels2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'I'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mvowels2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 元组不可变\n",
    "vowels2[2] = 'I'\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4e649e15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bdc96de",
   "metadata": {},
   "source": [
    "## *元组在小括号内至少要包含一个逗号*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3470d805",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('Python',)\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "bacbba2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Python',)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
